package com.pioneer.admin.controller.stock;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.stock.domain.StoInc;
import com.pioneer.web.stock.service.IStoIncService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 库存管理
 *
 * @author xx
 */
@RestController
@RequestMapping("/stock/increase")
public class StoIncController extends BaseController {

    @Resource
    private IStoIncService stoService;

    /**
     * 获取库存管理列表
     *
     * @param inc 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:list')")
    @GetMapping("/list")
    public AjaxResult list(StoInc inc) {
        boolean isPage = startPage();
        List<StoInc> list = stoService.list(Wrappers.query(inc));
        return success(isPage, list);
    }

    /**
     * 根据库存管理id获取详细信息
     *
     * @param incId 库存管理id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:query')")
    @GetMapping(value = "/{incId}")
    public AjaxResult getInfo(@PathVariable Long incId) {
        return AjaxResult.success(stoService.select(incId));
    }

    /**
     * 新增库存管理
     *
     * @param inc 库存管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:add')")
    @Log(title = "库存管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody StoInc inc) {
        System.out.println("调了add");
        return toAjax(stoService.insertInc(inc));
    }

    /**
     * 修改库存管理
     *
     * @param inc 库存管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:edit')")
    @Log(title = "库存管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody StoInc inc) {
        return toAjax(stoService.updateInc(inc));
    }

    /**
     * 删除库存管理
     *
     * @param incIds 库存管理ids组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:remove')")
    @Log(title = "库存管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{incIds}")
    public AjaxResult remove(@PathVariable Long[] incIds) {
        return toAjax(stoService.deleteStoInc(Arrays.asList(incIds)));
    }

    /**
     * 确认补充
     *
     * @param stoInc 库存管理
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('stock:increase:approve')")
    @Log(title = "库存管理", businessType = BusinessType.OTHER)
    @PostMapping("/approve")
    public AjaxResult approve(@Validated @RequestBody StoInc stoInc) {
        return toAjax(stoService.approveStoInc(stoInc));
    }

}
